package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.LoginDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = LoginService.class)
@Transactional
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginDao loginDao;

    public Result update(Member member){
        Member member1 = loginDao.getByPhoneNumber(member.getPhoneNumber());
        try {
            String phoneNumber = member.getPhoneNumber();
            loginDao.updateMember(member);
            Member getbyPhoneNumber = loginDao.getByPhoneNumber(phoneNumber);
            //throw new RuntimeException("111");
            return new Result(true,"修改成功",getbyPhoneNumber);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败，请稍后再试",member1);
        }
    }

    public Member getById(Integer id){
        return loginDao.getById(id);
    }

    // /、、、、、、、、、
    public Result getByMemberId(Integer memberId){
        try {
            List<Map> list = loginDao.getByMemberId(memberId);
            return new Result(true,"查找信息成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    public Result getByMemberIdAndStartDateAndendDate(Integer memberId,String startDate,String endDate){
        try {
            List<Map> list = loginDao.getByMemberIdAndStartDateAndendDate(memberId,startDate,endDate);
            return new Result(true,"查找信息成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    public Result getByMemberIdAndsetmealId(Integer memberId,Integer setmealId){
        try {
            List<Map> list = loginDao.getByMemberIdAndsetmealId(memberId,setmealId);
            return new Result(true,"查找信息成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    //////////////////////////////

    @Override
    public Result check(Map map) {

        //  validateCode  验证码的 key
        //  telephone  手机号的key

        return null;
    }

    @Override
    public Member getByPhoneNumber(String telephone) {
        return loginDao.getByPhoneNumber(telephone);
    }

    @Override
    public Integer insertMember(String  telephone) {
        return loginDao.insertMember(telephone);
    }
}
